package com.example.springmvcdemo.controller;


import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/login")
@Controller
public class LoginController {
    @RequestMapping("/check")
    @ResponseBody
    public boolean check(String userName, String password, HttpSession session) {
        //校验账号和密码是否为空
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }
        //校验账号和密码是否正确
        //一般使用常量来进行比较,如果使用变量的话,回抛出异常
        //防止空指针虽然前面进行了校验，，userName和password不可能为null，但是养成习惯
        if ("lisi".equals(userName) && "123".equals(password)) {
            session.setAttribute("userName", userName);
            return true;
        }
        return false;
    }

    @RequestMapping("/index")
    @ResponseBody
    public String index(HttpSession session) {
        String userName = (String)session.getAttribute("userName");
        return userName;
    }
}
